home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1997
/
MacHack 1997.toast
/
Hacks
/
Hacks ’95
/
Menu Controls
/
PatternMenu.cp
< prev
next >
Wrap
Text File
|
1995-06-24
|
14KB
|
451 lines
// Copyright © 1994-95 by Apple Computer, Inc. All rights reserved.
// PatternMenu.cp
/*
Change History:
02/22/95 srf 1222899 Use CopyPat when appropriate
02/22/95 srf 1222850 MacApp should use MABlockMove and MABlockMoveOverlap everywhere
*/
#ifndef __PATTERNMENU__
#include "PatternMenu.h"
#endif
// MacApp
#ifndef __UDIALOG__
#include <UDialog.h>
#endif
#ifndef __UWINDOW__
#include <UWindow.h>
#endif
#ifndef __UMACAPPUTILITIES__
#include <UMacAppUtilities.h>
#endif
// Toolbox
#ifndef __RESOURCES__
#include <Resources.h>
#endif
// DrawShapes
#ifndef __SHAPECOMMANDS__
#include "ShapeCommands.h"
#endif
#ifndef __UDRAWSHAPES__
#include "UDrawShapes.h"
#endif
//----------------------------------------------------------------------------------------
// Globals
TPatternsMenu* gPatternsMenu = NULL;
TWindow* gPatternsWindow = NULL;
Pattern gPat[kTotalPatterns]; // array of Patterns
//----------------------------------------------------------------------------------------
// InitPatternsMenu:
//----------------------------------------------------------------------------------------
#pragma segment AInit
void InitPatternsMenu()
{
// Copy patterns into global array of patterns
Handle patListHandle = GetResource('PAT#', 128);
if (patListHandle == NULL)
{
CopyPat(qd.white, gPat[0]);
CopyPat(qd.ltGray, gPat[1]);
CopyPat(qd.gray, gPat[2]);
CopyPat(qd.dkGray, gPat[3]);
CopyPat(qd.black, gPat[4]);
for (short i=5; i < kTotalPatterns; i++)
CopyPat(qd.black, gPat[i]);
}
else
{
short oldState = LockHandleHigh(patListHandle);
short* sizePtr = (short*)(*patListHandle);
long numPatterns = Min(kTotalPatterns, *sizePtr); // first word in pattern list is array size
// Calculate address where patterns start
Ptr address = (Ptr)((long)(*patListHandle) + sizeof(short));
for (short i=0; i < numPatterns; i++)
{
MABlockMove(address, &gPat[i], sizeof(Pattern));
address += sizeof(Pattern);
}
HSetState(patListHandle, oldState);
}
}
//========================================================================================
// CLASS TPatternsMenu
//========================================================================================
#undef Inherited
#define Inherited TTearOffMenuView
#pragma segment AInit
MA_DEFINE_CLASS_M1(TPatternsMenu, Inherited);
//----------------------------------------------------------------------------------------
// TPatternsMenu Constructor
//----------------------------------------------------------------------------------------
#pragma segment AInit
TPatternsMenu::TPatternsMenu()
{
}
//----------------------------------------------------------------------------------------
// TPatternsMenu::IPatternsMenu
//----------------------------------------------------------------------------------------
#pragma segment AInit
void TPatternsMenu::IPatternsMenu(ResNumber menuID)
{
this->ITearOffMenuView(menuID, kWPatternsChoice, kHPatternsChoice, gPatternsWindow);
TPatternsPalette* aPatternsPalette = new TPatternsPalette;
aPatternsPalette->IPatternsPalette(this);
aPatternsPalette->fIdentifier = 'MPLT'; // Identifies the patterns palette in the menu
}
//========================================================================================
// CLASS TPatternsPalette
//========================================================================================
#undef Inherited
#define Inherited TView
#pragma segment AOpen
MA_DEFINE_CLASS_M1(TPatternsPalette, Inherited);
//----------------------------------------------------------------------------------------
// TPatternsPalette Constructor
//----------------------------------------------------------------------------------------
#pragma segment AOpen
TPatternsPalette::TPatternsPalette()
{
fCurrPattern = 0; // the pattern currently selected by user
fOldPattern = 0; // the pattern previously selected by user
fSelectedPattern = 0; // the pattern selected by user during tracking
this->BuildChoiceArray();
this->BuildFramesArray();
}
//----------------------------------------------------------------------------------------
// TPatternsPalette::IPatternsPalette
//----------------------------------------------------------------------------------------
#pragma segment AOpen
void TPatternsPalette::IPatternsPalette(TView* itsSuperView)
{
VPoint itsSize(kWPatternsChoice, kHPatternsChoice);
this->IView(NULL, itsSuperView, gZeroVPt, itsSize, sizeFixed, sizeFixed);
}
//----------------------------------------------------------------------------------------
// TPatternsPalette::DoPostCreate
//----------------------------------------------------------------------------------------
#pragma segment AOpen
void TPatternsPalette::DoPostCreate(TDocument* itsDocument) // Override
{
Inherited::DoPostCreate(itsDocument);
this->AddAdorner(gSelectionAdorner, kDrawView, false); // wants DoHighlightSelection
}
//----------------------------------------------------------------------------------------
// TPatternsPalette::BuildChoiceArray
//----------------------------------------------------------------------------------------
#pragma segment AOpen
void TPatternsPalette::BuildChoiceArray()
{
// Create the Pattern choice rectangles that hold the Patterns
CRect r(0, 0, kWPatternsCell, kHPatternsCell);
short choiceID = 0;
for (short i=0; i<kPatternsAcross; i++) // all the rectangles across
{
for (short j=0; j<kPatternsDown; j++) // all the rectangles down
{
fChoiceArray[choiceID] = r;
OffsetRect(r, 0, kHPatternsCell + kFrameWidth);
choiceID++;
}
// Move rectangle over one column
SetRect(r, r.right + kFrameWidth, 0, r.right + kFrameWidth + kWPatternsCell, kHPatternsCell);
}
}
//----------------------------------------------------------------------------------------
// TPatternsPalette::BuildFramesArray
//----------------------------------------------------------------------------------------
#pragma segment AInit
void TPatternsPalette::BuildFramesArray()
{
// Build the vertical borders between the patterns
CRect tempRect(kWPatternsCell, 0, kWPatternsCell + kFrameWidth, kHPatternsChoice); /* left, top, right, bottom */
for (short i=0; i < kVerticalFrames; i++)
{
fFramesArray[i] = tempRect;
OffsetRect(tempRect, kWPatternsCell + kFrameWidth, 0);
}
// build the horizontal borders between the patterns
SetRect(tempRect, 0, kHPatternsCell, kWPatternsChoice, kHPatternsCell + kFrameWidth);
for (short j=0; j < kHorizontalFrames; j++)
{
fFramesArray[j + kVerticalFrames] = tempRect;
OffsetRect(tempRect, 0, kHPatternsCell + kFrameWidth);
}
}
//----------------------------------------------------------------------------------------
// TPatternsPalette::DoHighlightSelection
//----------------------------------------------------------------------------------------
#pragma segment ShapeRes
void TPatternsPalette::DoHighlightSelection(HLState /*fromHL*/, HLState toHL) // Override
{
if ((fCurrPattern >= 0) && (fCurrPattern < kTotalPatterns))
{
if (toHL == hlOff)
{
VRect area;
CRect r = fChoiceArray[fCurrPattern];
InsetRect(r, -5, -5);
this->QDToViewRect(r, area);
this->Draw(area);
}
else if (toHL == hlOn)
this->FrameCurrPattern();
}
}
//----------------------------------------------------------------------------------------
// TPatternsPalette::DoSetCursor
//----------------------------------------------------------------------------------------
#pragma segment ARes
void TPatternsPalette::DoSetCursor(const VPoint& /*localPoint*/, RgnHandle cursorRegion) // Override
{
CRect qdExtent;
SetCursor(&(qd.arrow));
this->GetQDExtent(qdExtent);
RectRgn(cursorRegion, qdExtent);
}
//----------------------------------------------------------------------------------------
// TPatternsPalette::Draw
//----------------------------------------------------------------------------------------
#pragma segment ARes
void TPatternsPalette::Draw(const VRect& area) // Override
{
CRect qdArea;
CRect r;
CRect tempRect;
CRect drawRect;
#if qDebug
this->AssumeFocused();
#endif
this->ViewToQDRect(area, qdArea);
// draw the frames
for (short i=0; i < kTotalFrames; i++)
{
r = fFramesArray[i];
if (SectRect(r, qdArea, drawRect))
FrameRect(drawRect);
}
// draw the Patterns
for (short j=0; j < kTotalPatterns; j++)
{
r = fChoiceArray[j];
if (SectRect(r, qdArea, drawRect))
FillRect(drawRect, &gPat[j]);
}
Inherited::Draw(area);
}
//----------------------------------------------------------------------------------------
// TPatternsPalette::FrameCurrPattern
//----------------------------------------------------------------------------------------
#pragma segment ShapeRes
void TPatternsPalette::FrameCurrPattern()
{
if (fCurrPattern != kNoPatternSelection) // frame selected pattern, if any
{
CRect r = fChoiceArray[fCurrPattern];
PenMode(patCopy);
InsetRect(r, - 1, - 1);
FrameRect(r); // frame it
InsetRect(r, - 3, - 3); // outer white frame
PenSize(3, 3);
PenPat(&qd.white);
FrameRect(r);
InsetRect(r, - 1, - 1); // outer black frame
PenSize(1, 1);
PenPat(&qd.black);
FrameRect(r);
}
}
//----------------------------------------------------------------------------------------
// TPatternsPalette::SelectNewPattern
//----------------------------------------------------------------------------------------
#pragma segment ShapeRes
void TPatternsPalette::SelectNewPattern(short whichPattern)
{
if (this->IsShown())
{
this->Focus();
this->DoHighlightSelection(hlOn, hlOff);
fCurrPattern = whichPattern;
this->DoHighlightSelection(hlOff, hlOn);
}
else
fCurrPattern = whichPattern; // so the correct pattern is selected when it is shown
}
//----------------------------------------------------------------------------------------
// TPatternsPalette::DoMouseCommand
//----------------------------------------------------------------------------------------
#pragma segment ASelCommand
void TPatternsPalette::DoMouseCommand(VPoint& theMouse,
TToolboxEvent* /*event*/,
CPoint /*hysteresis*/) // Override
{
// find the active TShapeView
TShapeView* aShapeView = NULL;
TWindow* aWindow = gApplication->GetActiveWindow(kNoFloaters);
if (aWindow)
aShapeView = (TShapeView*)aWindow->FindSubView('SHAP');
TPatternsPalette* menuPatternsView = NULL;
if (gPatternsMenu)
menuPatternsView = (TPatternsPalette*)gPatternsMenu->FindSubView('MPLT');
TPatternsPalette* floatingPatternsView = NULL;
if (gPatternsWindow)
floatingPatternsView = (TPatternsPalette*)gPatternsWindow->FindSubView('PPLT');
TReshadeCmd* aReshadeCmd = new TReshadeCmd;
aReshadeCmd->IReshadeCmd(cPatterns, aShapeView, this, menuPatternsView, floatingPatternsView, theMouse);
this->PostCommand(aReshadeCmd);
}
//----------------------------------------------------------------------------------------
// TPatternsPalette::TrackFeedback
//----------------------------------------------------------------------------------------
#pragma segment ShapeRes
void TPatternsPalette::TrackFeedback(TrackPhase aTrackPhase,
const VPoint& /*anchorPoint*/,
const VPoint& /*previousPoint*/,
const VPoint& nextPoint,
Boolean /*mouseDidMove*/,
Boolean turnItOn) // Override
{
CPoint qdPt;
CRect r;
if (aTrackPhase == trackBegin)
fSelectedPattern = kNoPatternSelection;
this->Focus();
qdPt = this->ViewToQDPt(nextPoint);
for (short i=0; i < kTotalPatterns; i++)
{
r = fChoiceArray[i];
if (fSelectedPattern == kNoPatternSelection)
InsetRect(r, -1, -1); // Inset the rect by the frame
if (PtInRect(qdPt, r) && (fSelectedPattern != i))
{
if (fSelectedPattern == kNoPatternSelection)
InsetRect(r, 1, 1); // Set it back for drawing
this->TurnOnPattern(r, i);
if (turnItOn)
{
if (fSelectedPattern != kNoPatternSelection)
{
r = fChoiceArray[fSelectedPattern];
this->TurnOffPattern(r, fSelectedPattern);
}
fSelectedPattern = i;
}
else
fSelectedPattern = kNoPatternSelection;
break;
}
}
if (aTrackPhase == trackEnd)
{
if (fSelectedPattern != kNoPatternSelection)
{
r = fChoiceArray[fSelectedPattern];
this->TurnOffPattern(r, fSelectedPattern);
}
}
}
//----------------------------------------------------------------------------------------
// TPatternsPalette::TurnOffPattern
//----------------------------------------------------------------------------------------
#pragma segment ShapeRes
void TPatternsPalette::TurnOffPattern(CRect patternRect, short whichPattern)
{
PenNormal();
FillRect(patternRect, &gPat[whichPattern]); // draw pattern
InsetRect(patternRect, - 1, - 1); // draw frame
FrameRect(patternRect);
// this->FrameCurrPattern(); // frame for current pattern
}
//----------------------------------------------------------------------------------------
// TPatternsPalette::TurnOnPattern
//----------------------------------------------------------------------------------------
#pragma segment ShapeRes
void TPatternsPalette::TurnOnPattern(CRect patternRect, short /*whichPattern*/)
{
PenNormal();
PenMode(patXor);
InsetRect(patternRect, - 1, - 1);
PenSize(2, 2);
FrameRect(patternRect);
}
//----------------------------------------------------------------------------------------
// RandomPattern
//----------------------------------------------------------------------------------------
#pragma segment ADoCommand
short RandomPattern()
{
return 1 + abs(Random() % kTotalPatterns);
}